package edu.wong.pro.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Context;

import edu.wong.pro.iservice.IPlanActivity;

public class ListActivityAction extends ActionSupport{
	
	private IPlanActivity iPlanActivity;
	
	public ListActivityAction(){
		super();
	}
	
	public IPlanActivity getiPlanActivity() {
		return iPlanActivity;
	}

	public void setiPlanActivity(IPlanActivity iPlanActivity) {
		this.iPlanActivity = iPlanActivity;
	}

	//添加学期活动计划
	public void addPlanList() throws IOException {
		//ActionContext context = ServletActionContext.getContext();
		
	//	HttpServletRequest request = ServletActionContext.getRequest();
		  ActionContext context=ActionContext.getContext(); 
		  HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST); 
		  HttpServletResponse response =(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);
		  PrintWriter printWriter = response.getWriter();
		  response.setCharacterEncoding("utf-8");
		  String year=request.getParameter("year");
		  String semester=request.getParameter("semester"); 
		  String theme=request.getParameter("theme"); 
		  int id =  iPlanActivity.addPlanActivity(year, semester, theme);
		  System.out.println("id:"+id);
		  JSONObject jObject= new JSONObject();
		  jObject.put("ids", id);
		  printWriter.print(jObject);
		  printWriter.flush();
		  printWriter.close();
	}
	
	public void addPlanActivity(){
		ActionContext context=ActionContext.getContext(); 
		HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST); 
		 String listid = request.getParameter("listID");
		 String date = request.getParameter("date");
		 String theme = request.getParameter("theme");
		 String addr = request.getParameter("addr");
		 iPlanActivity.savePlanActivity(listid, date, theme, addr);
		 System.out.println(listid+date+theme+addr);
	}
	
	//获取计划活动列表
	public  void getPlanActivityList() throws IOException{
		ActionContext context=ActionContext.getContext(); 
		HttpServletResponse response =(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);  
		response.setCharacterEncoding("utf-8");
		JSONArray array = iPlanActivity.getPlanActivityList();
		PrintWriter writer = response.getWriter();
		writer.print(array);
		writer.flush();
		writer.close();
				
	}
	//获取当前计划活动列表的计划活动
	public void getPlanActivity() throws IOException{
		ActionContext context=ActionContext.getContext(); 
		HttpServletResponse response =(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);  
		HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST); 
		int listId = Integer.parseInt(request.getParameter("listId"));
		response.setCharacterEncoding("utf-8");
		JSONArray array = iPlanActivity.getPlanActivity(listId);
		PrintWriter writer = response.getWriter();
		writer.print(array);
		writer.flush();
		writer.close();
	}
}
